package com.ang.juc.nt.atomicInteger;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;

public class AtomicIntegerTest {

    static Integer count = 0;

    static AtomicInteger countAtomic = new AtomicInteger();

    public static void main(String[] args) throws InterruptedException {

        List<Thread> listA = new ArrayList<>();
        IntStream.range(0,10000).forEach(i->{
            Thread thread = new Thread(() -> {
                count ++;
            }, "ThreadA#" + i);

            thread.start();
            listA.add(thread);
        });
        for (Thread thread : listA) {
            thread.join();
        }

        System.out.println("count:"+count);


        List<Thread> listB = new ArrayList<>();
        IntStream.range(0,10000).forEach(i->{
            Thread thread = new Thread(() -> {
                countAtomic.addAndGet(1);
            }, "ThreadB#" + i);

            thread.start();
            listB.add(thread);
        });
        for (Thread thread : listB) {
            thread.join();
        }

        System.out.println("countAtomic:"+countAtomic);
    }
}
